//
//  MoreController.m
//  zhangchu
//
//  Created by aoyolo on 15/11/12.
//  Copyright © 2015年 李祖铜. All rights reserved.
//

#import "MoreController.h"
#import "HotMoreController.h"
#import "NewMoreViewController.h"

@interface MoreController ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)NSMutableArray *childViews;


@end

@implementation MoreController
{
    UIView *titleView;
    NSArray *titlearray;
    UILabel *label;
    UIButton *btnBon;
    UIButton *tmBtn;
}

-(NSMutableArray *)childViews
{
    if (!_childViews) {
        _childViews = [NSMutableArray array];
    }
    return _childViews;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView];
    [self setupScorll];
    
}

-(void)setupTitleView
{
    titleView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 32)];
    titlearray = @[@"热门", @"新品"];
    label = [[UILabel alloc]initWithFrame:CGRectMake(0, 31, 60, 2)];
    label.backgroundColor = [UIColor redColor];
    
    
    for (int i = 0; i <titlearray.count; i++)
    {
        btnBon = [UIButton buttonWithType:UIButtonTypeCustom];
        btnBon.frame = CGRectMake(i*60, 0, 60, 30);
        btnBon.tag = 100+i;
        [btnBon setTitle:titlearray[i] forState:UIControlStateNormal];
        [btnBon setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btnBon addTarget:self action:@selector(changeviewBtn:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:btnBon];
    }
    self.navigationItem.titleView = titleView;
    [titleView addSubview:label];
}

-(void)setupScorll
{
    UIView *lack = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:lack];
    HotMoreController *hotCtl = [[HotMoreController alloc]init];
    hotCtl.view.backgroundColor = [UIColor redColor];
    [self addChildViewController:hotCtl];
    
    NewMoreViewController *newCtl = [[NewMoreViewController alloc]init];
    newCtl.view.backgroundColor = [UIColor orangeColor];
    [self addChildViewController:newCtl];
    [self.childViews addObject:hotCtl.view];
    [self.childViews addObject:newCtl.view];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight)];
    scrollView.delegate =self;
    [self.view addSubview:scrollView];
    for (int i = 0; i <self.childViews.count; i++)
    {
        CGFloat childVX = ScreenWidth*i;
        UIView *childV = self.childViews[i];
        childV.frame = CGRectMake(childVX, 0, ScreenWidth, ScreenHeight);
        [scrollView addSubview:childV];
    }
    
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(ScreenWidth*2, 0);
    scrollView.bounces = NO;
    scrollView.delegate = self;
    self.scrollView = scrollView;
    
}

-(void)changeviewBtn:(UIButton *)sender
{
    NSInteger index = sender.tag -100;
    [_scrollView setContentOffset:CGPointMake(index*ScreenWidth, 0) animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger offset = scrollView.contentOffset.x/ScreenWidth;
   
    NSLog(@"%ld", (long)offset);
    if (offset ==0)
    {
        [self objectAtIndex:0];
    }if (offset == 1) {
        [self objectAtIndex:1];
    }
}

-(void)objectAtIndex:(NSInteger)index
{
    UIButton *sender = [titleView.subviews objectAtIndex:index];
    if (tmBtn !=nil)
    {
        tmBtn.selected = NO;
    }
    if (!sender.selected)
    {
        sender.selected = YES;
    }
    tmBtn = sender;
    [UIView animateWithDuration:0.3 animations:^{
        label.center = CGPointMake(sender.center.x, 31);
    }];
}

@end
